package com.bot.nft.module.dc.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bot.common.exception.CommonException;
import com.bot.dev.api.DevBlockChainApi;
import com.bot.nft.core.enums.chain.ChainLogEnum;
import com.bot.nft.core.enums.op.SyndropEnum;
import com.bot.nft.module.dc.domain.*;
import com.bot.nft.module.dc.domain.vo.DcSellOrderInfoVo;
import com.bot.nft.module.dc.domain.vo.SellGetInfoVo;
import com.bot.nft.module.dc.domain.vo.SellOrdPayLogVo;
import com.bot.nft.module.dc.domain.vo.SellPoInfoVo;
import com.bot.nft.module.dc.mapper.*;
import com.bot.nft.module.dc.service.IDcNftCertService;
import com.bot.nft.module.dc.service.IDcSellOrderInfoService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author RenXiaoZheng
 * @since 2024-01-24    8 9 17 16 15 14
 */
@Service
public class DcSellOrderInfoServiceImpl implements IDcSellOrderInfoService {
    @Resource
    private DcSellOrderPayLogMapper dcSellOrderPayLogMapper;

    @Resource
    private DcSellOrderInfoMapper dcSellOrderInfoMapper;

    @Resource
    private DcSellInfoMapper dcSellInfoMapper;

    @Resource
    private DcFlowLogMapper dcFlowLogMapper;

    @Resource
    private DcNftCertMapper dcNftCertMapper;

    @Resource
    private DcSellTradeMapper dcSellTradeMapper;


    @Resource
    private IDcNftCertService dcNftCertService;

    /**
     * 分页查询寄售订单
     *
     * @param status   订单状态
     * @param phone    手机号
     * @param pageNo   页码
     * @param pageSize 条数
     * @return
     */
    @Override
    public Page<SellPoInfoVo> querySellPo(Integer status, String phone, Integer pageNo, Integer pageSize) {
        //创建page分页
        Page<SellPoInfoVo> page = new Page<>(pageNo, pageSize);
        List<SellPoInfoVo> sellPoInfoVos = dcSellOrderInfoMapper.selectSellOrder(page, status, phone);
        page.setRecords(sellPoInfoVos);
        return page;
    }

    /**
     * 查看寄售订单信息
     *
     * @param id 订单信息标识
     * @return
     */
    @Override
    public SellGetInfoVo getSellPo(String id) {
        SellGetInfoVo sellPoInfoVo = dcSellOrderInfoMapper.lookSellOrder(id);
        return sellPoInfoVo;
    }

    /**
     * 查看订单支付信息
     *
     * @param id 订单信息标识
     * @return
     */
    @Override
    public List<SellOrdPayLogVo> getSellOrder(String id) {
        QueryWrapper<DcSellOrderPayLog> dcSellOrderPayLogQueryWrapper = new QueryWrapper<>();
        dcSellOrderPayLogQueryWrapper.lambda().eq(DcSellOrderPayLog::getSellId, id);
        List<SellOrdPayLogVo> sellOrdPayLogVos = dcSellOrderPayLogMapper.selectVoList(dcSellOrderPayLogQueryWrapper);
        return sellOrdPayLogVos;
    }

    /**
     * 购买藏品
     *
     * @param sellId 寄售藏品信息标识
     * @param mbId   购买人标识
     * @return
     */
    @Override
    public String placeOrder(String sellId, String mbId) {
        //购买订单时添加藏品信息跟藏品
        QueryWrapper<DcSellInfo> dcSellInfoQueryWrapper = new QueryWrapper<>();
        dcSellInfoQueryWrapper.lambda().eq(DcSellInfo::getId, sellId);
        DcSellInfo dcSellInfo = dcSellInfoMapper.selectOne(dcSellInfoQueryWrapper);
        //生成订单信息
        DcSellOrderInfo dcSellOrderInfo = new DcSellOrderInfo();
        dcSellOrderInfo.setId(IdUtil.getSnowflakeNextIdStr());
        dcSellOrderInfo.setSellId(sellId);
        dcSellOrderInfo.setSellerId(dcSellInfo.getMbId());
        dcSellOrderInfo.setBuyersId(mbId);
        dcSellOrderInfo.setNftCertId(dcSellInfo.getCertId());
        dcSellOrderInfo.setNftSellPrice(dcSellInfo.getPrice());
        dcSellOrderInfo.setOrdersAmount(dcSellInfo.getPrice());
        dcSellOrderInfo.setStatus(SyndropEnum.TO_BE_PAID.getValue());
        dcSellOrderInfo.setCreateTime(new Date());
        //保存订单信息
        dcSellOrderInfoMapper.insert(dcSellOrderInfo);
        //修改寄售藏品信息
        dcSellInfo.setStatus(SyndropEnum.LOCKED.getValue());
        dcSellInfoMapper.updateById(dcSellInfo);
        return dcSellOrderInfo.getId();
    }


    /**
     * 支付寄售订单
     *
     * @param orderId 寄售藏品信息标识
     * @param mbId    会员Id
     * @return
     */
    @Override
    public Boolean payOrder(String orderId, String mbId) {
        //寄售订单信息
        QueryWrapper<DcSellOrderInfo> dcSellInfoQueryWrapper = new QueryWrapper<>();
        dcSellInfoQueryWrapper.lambda().eq(DcSellOrderInfo::getId, orderId);
        DcSellOrderInfoVo dcSellOrderInfoVo = dcSellOrderInfoMapper.selectVoOne(dcSellInfoQueryWrapper);
        //判断时间是否过期
        //寄售订单的状态改为已完成
        LambdaUpdateWrapper<DcSellOrderInfo> dcSellInfoUpdateWrapper = new LambdaUpdateWrapper<>();
        dcSellInfoUpdateWrapper.eq(DcSellOrderInfo::getId, orderId);
        dcSellInfoUpdateWrapper.set(DcSellOrderInfo::getStatus, SyndropEnum.DONE.getValue());
        dcSellOrderInfoMapper.update(null, dcSellInfoUpdateWrapper);
        //转让藏品证书
        dcNftCertService.transferCert(dcSellOrderInfoVo.getNftCertId(), mbId,
                ChainLogEnum.BUSINESS_MARKET_PURCHASE.getValue());
        //把藏品证书信息的状态改为0，当前藏品的会员Id转成当前会员
        LambdaUpdateWrapper<DcNftCert> dcNtfCertUpdateWrapper = new LambdaUpdateWrapper<>();
        dcNtfCertUpdateWrapper.eq(DcNftCert::getId, dcSellOrderInfoVo.getNftCertId());
        dcNtfCertUpdateWrapper.set(DcNftCert::getStatus, SyndropEnum.HOIDING.getValue()).set(DcNftCert::getMbId, mbId);
        dcNftCertMapper.update(null, dcNtfCertUpdateWrapper);
        //寄售藏品信息的状态改为已售出
        LambdaUpdateWrapper<DcSellInfo> dcSellInfoLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        dcSellInfoLambdaUpdateWrapper.eq(DcSellInfo::getId, dcSellOrderInfoVo.getSellId());
        dcSellInfoLambdaUpdateWrapper.set(DcSellInfo::getStatus, SyndropEnum.SOLD.getValue());
        dcSellInfoMapper.update(null, dcSellInfoLambdaUpdateWrapper);
        //生成寄售订单支付信息
        DcSellOrderPayLog sellOrdPayLogBo = new DcSellOrderPayLog();
        sellOrdPayLogBo.setId(IdUtil.getSnowflakeNextIdStr());
        sellOrdPayLogBo.setMemberId(mbId);
        sellOrdPayLogBo.setTraSerNum(IdUtil.getSnowflakeNextIdStr());
//        sellOrdPayLogBo.setPayManner();
//        sellOrdPayLogBo.setPayAccoun();
//        sellOrdPayLogBo.setPayType();
//        sellOrdPayLogBo.setStatus();
        sellOrdPayLogBo.setPaymentTime(new Date());
        sellOrdPayLogBo.setPaymentAmount(dcSellOrderInfoVo.getPayAmount());
        sellOrdPayLogBo.setSellId(orderId);
        int insert = dcSellOrderPayLogMapper.insert(sellOrdPayLogBo);
        //生成寄售订单交易流水信息标识信息
        DcSellTrade sellTradeLogBo = new DcSellTrade();
        sellTradeLogBo.setId(IdUtil.getSnowflakeNextIdStr());
        sellTradeLogBo.setSellId(orderId);
        sellTradeLogBo.setPayerId(dcSellOrderInfoVo.getBuyersId());
//        sellTradeLogBo.setPayerAccount();
        sellTradeLogBo.setPayeeId(dcSellOrderInfoVo.getSellerId());
        sellTradeLogBo.setPayerAmount(BigDecimal.valueOf(Long.parseLong(dcSellOrderInfoVo.getPayAmount())));
//        sellTradeLogBo.setPlatformFees();
//        sellTradeLogBo.setPayeeAmount();
        sellTradeLogBo.setPayerPayTime(new Date());
//        sellTradeLogBo.setPayeeArrTime();
        int insert1 = dcSellTradeMapper.insert(sellTradeLogBo);
        //生成流转记录
        DcFlowLog dcFlowLogBo = new DcFlowLog();
        dcFlowLogBo.setId(IdUtil.getSnowflakeNextIdStr());
        dcFlowLogBo.setCertId(dcSellOrderInfoVo.getNftCertId());
        dcFlowLogBo.setMbId(mbId);
        dcFlowLogBo.setCirculation(SyndropEnum.PURCHASE.getValue());
        dcFlowLogBo.setFlowTime(new Date());
        int insert2 = dcFlowLogMapper.insert(dcFlowLogBo);
        return insert2 > 0;
    }

    /**
     * 取消寄售订单
     *
     * @param orderId 订单标识
     * @return 成功，失败
     */
    @Override
    public Boolean cancelPayOrder(String orderId) {
        Boolean flag = null;
        //查询订单信息
        LambdaQueryWrapper<DcSellOrderInfo> orderWrapper = new LambdaQueryWrapper<>();
        orderWrapper.eq(DcSellOrderInfo::getId, orderId);
        DcSellOrderInfo dcSellOrderInfo = dcSellOrderInfoMapper.selectOne(orderWrapper);
        if (ObjectUtils.isEmpty(dcSellOrderInfo)) throw new CommonException("未查询到订单");
        //修改寄售订单状态为已取消
        dcSellOrderInfo.setStatus(SyndropEnum.CANCELED.getValue());
        int update = dcSellOrderInfoMapper.updateById(dcSellOrderInfo);
        if (update > 0) flag = true;
        //修改寄售藏品信息状态为售卖中
        LambdaUpdateWrapper<DcSellInfo> dcSellInfoUpdateWrapper = new LambdaUpdateWrapper<>();
        dcSellInfoUpdateWrapper.eq(DcSellInfo::getId, dcSellOrderInfo.getSellId());
        dcSellInfoUpdateWrapper.set(DcSellInfo::getStatus, SyndropEnum.ON_SALET.getValue());
        dcSellInfoMapper.update(null, dcSellInfoUpdateWrapper);


        return flag;
    }


    /**
     * 根据订单标识和会员标识查看藏品订单
     *
     * @param orderId 订单标识
     * @return 订单信息vo
     */
    @Override
    public DcSellOrderInfoVo viewOrder(String orderId) {

        return dcSellOrderInfoMapper.getOrderInfo(orderId);
    }

    /**
     * 查看寄售订单信息集合
     *
     * @return
     */
    @Override
    public List<DcSellOrderInfo> selectSellOrder() {
        QueryWrapper<DcSellOrderInfo> dcSellOrderInfoQueryWrapper = new QueryWrapper<>();
        dcSellOrderInfoQueryWrapper.lambda().eq(DcSellOrderInfo::getStatus, "2");
        List<DcSellOrderInfo> dcSellOrderInfos = dcSellOrderInfoMapper.selectList(dcSellOrderInfoQueryWrapper);
        return dcSellOrderInfos;
    }

}
